package com.hx.update.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.lib_hotupdate.R;

import java.util.Locale;

public class UpdateDialog extends AlertDialog {

    private UpdateDialogListener updateDialogListener;
    private String apkVersion;//apk版本
    private long apkSize;//apk大小
    private int apkStatus;//是否强制更新 1强制

    public UpdateDialog(Context context) {
        super(context, R.style.base_dialog_loading);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_dialog_update);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        init();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void init() {
        ImageView ivClose = findViewById(R.id.iv_close);
        TextView tvVersion = findViewById(R.id.tv_version);
        TextView tvSize = findViewById(R.id.tv_size);
        TextView tvUpdate = findViewById(R.id.tv_update);
        TextView tvIgnore = findViewById(R.id.tv_ignore);

        tvVersion.setText(String.format(Locale.getDefault(), "最新版本：V%s", apkVersion));
        tvSize.setText(String.format(Locale.getDefault(), "文件大小：%.2fMB", (double) apkSize / (1024 * 1024)));

        if (apkStatus == 1) {
            ivClose.setVisibility(View.GONE);
            tvIgnore.setVisibility(View.GONE);
        }

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (updateDialogListener != null) {
                    updateDialogListener.onClose();
                }
                dismiss();
            }
        });

        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (updateDialogListener != null) {
                    updateDialogListener.onUpgrade();
                }
                dismiss();
            }
        });

        tvIgnore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (updateDialogListener != null) {
                    updateDialogListener.onIgnore();
                }
                dismiss();
            }
        });
    }

    public UpdateDialog setApkVersion(String version) {
        this.apkVersion = version;
        return this;
    }

    public UpdateDialog setApkSize(long size) {
        this.apkSize = size;
        return this;
    }

    public UpdateDialog setApkStatus(int status) {
        this.apkStatus = status;
        return this;
    }

    public UpdateDialog setClickListener(UpdateDialogListener listener) {
        this.updateDialogListener = listener;
        return this;
    }

    public interface UpdateDialogListener {
        void onUpgrade();       //升级

        void onIgnore();        //忽略

        void onClose();         //关闭
    }
}